"""
Flask应用主入口 - API路由定义
"""
from flask import Flask, jsonify
from flask_cors import CORS
import api_service

app = Flask(__name__)
CORS(app)  # 允许跨域请求

# ==================== 用户认证API ====================

@app.route('/api/login', methods=['POST'])
def login():
    """用户登录"""
    return api_service.login()

@app.route('/api/user/info', methods=['GET'])
def get_user_info():
    """获取用户信息接口"""
    return api_service.get_user_info()

@app.route('/api/logout', methods=['POST'])
def logout():
    """注销接口"""
    return api_service.logout()

@app.route('/api/health', methods=['GET'])
def health_check():
    """健康检查接口"""
    return api_service.health_check()

# ==================== 文本分析相关API ====================

@app.route('/api/analyze', methods=['POST'])
def analyze_text():
    """文本分析接口 - 智能设备交互意图分类"""
    return api_service.analyze_text()

# ==================== 训练数据探索API ====================

@app.route('/api/explore/overview', methods=['GET'])
def explore_data_overview():
    """训练数据概览接口"""
    return api_service.explore_data_overview()

@app.route('/api/explore/samples', methods=['GET'])
def get_training_samples():
    """获取训练样本数据（支持分页和筛选）"""
    return api_service.get_training_samples()

# ==================== 模型分析API ====================

@app.route('/api/analysis/training-history', methods=['GET'])
def get_training_history():
    """获取模型训练历史数据"""
    return api_service.get_training_history()

@app.route('/api/analysis/model-performance', methods=['GET'])
def get_model_performance():
    """获取模型性能对比数据"""
    return api_service.get_model_performance()

@app.route('/api/analysis/confusion-matrix', methods=['GET'])
def get_confusion_matrix():
    """获取混淆矩阵数据"""
    return api_service.get_confusion_matrix()

# ==================== 统计数据API ====================

@app.route('/api/statistics/kpi', methods=['GET'])
def get_kpi_statistics():
    """获取KPI统计数据"""
    return api_service.get_kpi_statistics()

@app.route('/api/statistics/realtime-predictions', methods=['GET'])
def get_realtime_predictions():
    """获取实时预测数据流"""
    return api_service.get_realtime_predictions()

@app.route('/api/statistics/category-distribution', methods=['GET'])
def get_category_distribution():
    """获取分类分布统计"""
    return api_service.get_category_distribution()

# ==================== 错误处理 ====================

@app.errorhandler(404)
def not_found(error):
    return jsonify({
        'success': False,
        'message': '接口不存在'
    }), 404

@app.errorhandler(500)
def internal_error(error):
    return jsonify({
        'success': False,
        'message': '服务器内部错误'
    }), 500

# ==================== 应用启动 ====================

if __name__ == '__main__':
    print("Flask API服务启动中...")
    print("服务地址: http://localhost:5000")
    print("可用接口:")
    print("   === 用户认证 ===")
    print("   POST /api/login - 用户登录")
    print("   GET  /api/user/info - 获取用户信息")
    print("   POST /api/logout - 用户注销")
    print("   GET  /api/health - 健康检查")
    print("   === 文本分析 ===")
    print("   POST /api/analyze - 智能设备交互文本分析")
    print("   === 数据探索 ===")
    print("   GET  /api/explore/overview - 训练数据概览")
    print("   GET  /api/explore/samples - 获取训练样本（分页）")
    print("   === 模型分析 ===")
    print("   GET  /api/analysis/training-history - 训练历史")
    print("   GET  /api/analysis/model-performance - 模型性能对比")
    print("   GET  /api/analysis/confusion-matrix - 混淆矩阵")
    print("   === 统计数据 ===")
    print("   GET  /api/statistics/kpi - KPI统计数据")
    print("   GET  /api/statistics/realtime-predictions - 实时预测数据流")
    print("   GET  /api/statistics/category-distribution - 分类分布统计")
    print("\n注意：除了 /api/health 外，所有接口都需要认证")
    app.run(debug=True, host='0.0.0.0', port=5000)